package eduware.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

import eduware.util.AWTUtilitiesWrapper;

/**
 * This class represents a Outputfield which contain a Textfield which shows
 * what he says
 * 
 * @author Dennis Gruner
 * 
 */
public class OutputField extends JDialog implements ActionListener {

	/**
	 * Generated Version ID
	 */
	private static final long serialVersionUID = -8920795452121250787L;

	JTextArea outputTextField;
	JButton okButton;
	String inputText;

	Integer direction = 1;

	public OutputField() {

		// Configuration for the Content Pane

		this.setLayout(null);

		this.setMinimumSize(new Dimension(250, 160));

		this.setUndecorated(true);

		this.setResizable(false);

		outputTextField = new JTextArea(3, 30);
		outputTextField.setWrapStyleWord(true);
		outputTextField.setLineWrap(true);
		Border thickBorder = new LineBorder(Color.gray, 2);
		outputTextField.setBorder(thickBorder);
		outputTextField.setBounds(15, 15, 220, 70);
		outputTextField.setEditable(false);
		this.getContentPane().add(outputTextField);

		okButton = new JButton("Ok");
		okButton.addActionListener(this);
		okButton.setBounds(15, 90, 220, 20);
		this.getContentPane().add(okButton);

		AWTUtilitiesWrapper.setWindowOpacity(this, 0.9f);

		this.setAlwaysOnTop(true);

		AWTUtilitiesWrapper.setWindowShape(OutputField.this, GUIUtils
				.getSpeechBubbleShape(getWidth(), getHeight()));

		this.getContentPane().setBackground(new Color(255, 254, 179));

		this.setVisible(false);
	}

	/**
	 * Fade in of the InputField and sets the location
	 * 
	 * @param width
	 * @param height
	 */
	public void showMessage(int width, int height, String msg, Color color) {
		if (msg == null && color == null) {
			return;
		} else {
			this.setVisible(true);
			outputTextField.setText(msg);
			outputTextField.setForeground(color);
			this.setLocation(width, height);
			if (AWTUtilitiesWrapper.isTranslucencySupported()) {
				float opacity = 0.0f;
				while (opacity < 0.9) {
					AWTUtilitiesWrapper.setWindowOpacity(this, opacity);
					opacity += 0.005f;
				}
			}
		}
	}

	/**
	 * Fade Out of the Inputfield
	 */
	public void fadeOutOutputFiled() {

		if (AWTUtilitiesWrapper.isTranslucencySupported()) {
			float opacity = 0.9f;
			while (opacity < 0.0f) {
				AWTUtilitiesWrapper.setWindowOpacity(this, opacity);
				opacity -= 0.005f;
			}
		}
		this.setVisible(false);
	}

	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == this.okButton) {
			inputText = outputTextField.getText();
			fadeOutOutputFiled();
		}
	}

	/************************************
	 * function to reset the shape of the window based on the internal direction
	 * value
	 */
	public void reshape() {

		// the speech bubble should be displayed on the right hand side
		if (this.direction == 1) {
			AWTUtilitiesWrapper.setWindowShape(OutputField.this, GUIUtils
					.getSpeechBubbleShape(getWidth(), getHeight()));
		} else {
			// the speech bubble should be displayed on the left
			AWTUtilitiesWrapper.setWindowShape(OutputField.this, GUIUtils
					.getSpeechBubbleShape2(getWidth(), getHeight()));
		}
	}

	public void setDirection(Integer direction) {
		this.direction = direction;
	}

	public Integer getDirection() {
		return direction;
	}
	
	public void clearInput() {
		this.outputTextField.setText("");
	}

}